﻿#pragma execution_character_set("utf-8")
#include "studentscore.h"
#include <QHeaderView>
#include <QDebug>
#include "src/database/cdatabase.h"
#include "third/singleton_cpp/include/singleton.h"

StudentScore::StudentScore(QWidget *parent)
    : QWidget{parent}
    ,m_pTableWidget(nullptr)
{
    initUI();
    initData();
}

StudentScore::~StudentScore()
{

}

QVector<int> StudentScore::get_score()
{
    std::vector<DB::students>students_data = singleton<CDataBase>().getDB()->query<DB::students>();

    QVector<int>ret_vec;
    ret_vec<<students_data[0].chinese<<students_data[1].math<<students_data[2].english;

    return ret_vec;
}

void StudentScore::initUI()
{
    m_pTableWidget = new QTableWidget(this);

    QStringList headers;
    headers<<"学号"<<"姓名"<<"性别"<<"年龄"<<"语文/成绩"<<"语文考试时间"<<"数学/成绩"<<"数学考试时间"<<"英语/成绩"
            <<"英语考试时间";
    m_pTableWidget->setColumnCount(headers.size());
    m_pTableWidget->setHorizontalHeaderLabels(headers);
    m_pTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    QHBoxLayout* h_layout = new QHBoxLayout();
    h_layout->addWidget(m_pTableWidget);
    setLayout(h_layout);

}

void StudentScore::initData()
{
    std::vector<DB::students>students_data = singleton<CDataBase>().getDB()->query<DB::students>();
    m_pTableWidget->setRowCount(students_data.size());
    int row = 0;
    for(auto iter = students_data.begin();iter != students_data.end();iter++)
    {
        QTableWidgetItem* p_item = nullptr;
        p_item = new QTableWidgetItem();
        p_item->setData(Qt::EditRole,iter->ID);
        m_pTableWidget->setItem(row,0,p_item);

        p_item = new QTableWidgetItem(iter->name.c_str());
        m_pTableWidget->setItem(row,1,p_item);

        p_item = new QTableWidgetItem(iter->sex.c_str());
        m_pTableWidget->setItem(row,2,p_item);

        p_item = new QTableWidgetItem();
        p_item->setData(Qt::EditRole,iter->age);
        m_pTableWidget->setItem(row,3,p_item);

        p_item = new QTableWidgetItem(iter->chinese);
        p_item->setData(Qt::EditRole,iter->chinese);
        m_pTableWidget->setItem(row,4,p_item);

        p_item = new QTableWidgetItem(iter->chinese_time.c_str());
        m_pTableWidget->setItem(row,5,p_item);

        p_item = new QTableWidgetItem(iter->math);
        p_item->setData(Qt::EditRole,iter->math);
        m_pTableWidget->setItem(row,6,p_item);
        p_item = new QTableWidgetItem(iter->math_time.c_str());
        m_pTableWidget->setItem(row,7,p_item);

        p_item = new QTableWidgetItem(iter->english);
        p_item->setData(Qt::EditRole,iter->english);
        m_pTableWidget->setItem(row,8,p_item);

        p_item = new QTableWidgetItem(iter->english_time.c_str());
        m_pTableWidget->setItem(row,9,p_item);
        row++;
    }
}
